package dao;

import bean.News;
import bean.NewsofUser;
import utils.DbUtil;

import java.util.List;

public class NewsDao {
    public static boolean Insert(String title,String content,String userid,String pubtime,String headimg)
    {
        String sql="insert into news(title,content,userid,pubtime,headimg) values(?,?,?,?,?)";
        int row= DbUtil.getInstance().execute(sql, new Object[]{title,content,userid,pubtime,headimg});
        if(row>0)
            return true;
        return false;
    }
    public static boolean Delete(int id)
    {
        String sql="delete from news where id=?";
        int row= DbUtil.getInstance().execute(sql, new Object[]{id});
        if(row>0)
            return true;
        return false;
    }
    public static News Update(int id)
    {
        String sql="select * from news where id=?";
        News newslist=  (News) DbUtil.getInstance().queryObject(sql,new Object[]{id}, News.class);
        System.out.println(newslist.toString());
        return newslist;
    }
    public static boolean Update1(int id ,String title,String content,String userid,String pubtime,String headimg)
    {
        headimg+="";
        String sql="update news set title=? ,content = ?,userid = ?,pubtime=? ,headimg = ?where id=?";
        int row   =   DbUtil.getInstance().execute(sql,new Object[]{title,content,userid,pubtime,headimg,id});
        if(row>0)
            return true;
        return false;

    }
    public static List<NewsofUser> select(){

        List<NewsofUser> list=  DbUtil.getInstance().queryList("SELECT * FROM news_info_view",null, NewsofUser.class);
        for (NewsofUser user : list) {
            System.out.println(user.toString());
        }
        return list;
    }
    public static Object[] select(int id){

        NewsofUser news= (NewsofUser) DbUtil.getInstance().queryObject("SELECT  newstitle ,newscontent ,username,pubtime ,newsimg FROM news_info_view where newsid = ?",new Object[]{id}, NewsofUser.class);

        System.out.println(news.toString());
        Object[] news1 =new Object[5];
        news1[0]=news.getNewstitle();
        news1[1]=news.getNewscontent();
        news1[2]=news.getUsername();
        news1[3]=news.getPubtime();
        news1[4]=news.getNewsimg();

        System.out.println("1:"+news1[0].toString());
        System.out.println("2:"+news1[1].toString());
        System.out.println("3:"+news1[3].toString());
        System.out.println("4:"+news1[4].toString());
        return news1;
    }
    public static List<NewsofUser> select(String message){
        String message1="%"+message+"%";
        List<NewsofUser> list=  DbUtil.getInstance().queryList("SELECT * FROM news_info_view where newsid like ? or newstitle like ? or username like ?   ",new Object[]{message1,message1,message1}, NewsofUser.class);
        for (NewsofUser news : list) {
            System.out.println(news.toString());
        }
        return list;
    }
}
